package com.qfedu.scode.beetl;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.StringTemplateResourceLoader;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @program: StartCoding
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-01-05 09:55
 */
public class BeetlStudy01 {
    public static void main(String[] args) throws IOException {
       render3();
    }
    public static void render1() throws IOException {
        //初始化代码
        //实例化加载器
        StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
        //实例化配置对象
        Configuration cfg = Configuration.defaultConfiguration();
        //实例化模板组对象
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        //获取模板
        Template t = gt.getTemplate("hello,${name}");
        //为模板中的变量，赋值
        t.binding("name", "Java");
        //渲染结果
        String str = t.render();
        System.out.println(str);
    }
    public static void render2() throws IOException {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("com/qfedu/scode/beetl/");
        System.out.println(resourceLoader.getInfo());
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate("/hello.btl");
        Map<String,String> map=new HashMap<>();
        map.put("str1","醒醒吧");
        map.put("num1","886");
        t.binding(map);
        t.renderTo(new FileOutputStream("hello-2.txt"));
    }
    public static void render3() throws IOException {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("com/qfedu/scode/beetl/");
        System.out.println(resourceLoader.getInfo());
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate("/names.btl");
        Map<String,Object> map=new HashMap<>();
        ArrayList<Student> list=new ArrayList<>();
        for(int i=1;i<65;i++){
            //list.add(new Student(i,"Java学员-"+i,"杭州",new Random().nextInt(10000)+10000));
        }
        map.put("title","ZZ-Java-2008花名册");
        map.put("students",list);
        t.binding(map);
        t.renderTo(new FileOutputStream(map.get("title")+".html"));
    }
}
